package com.example.springboot.topic;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Topic Exchange（主题模式）
 * topic 是RabbitMQ中最灵活的一种方式，可以根据routing_key自由的绑定不同的队列
 * 配置队列，绑定交换机
 * @program: 12-springboot-rabbitmq
 * @description: TopicRabbitConfig
 * @author: Yuwl
 * @create: 2019-12-27 17:11
 **/
@Configuration
public class TopicRabbitConfig {

	final static String message = "q_topic_message";
	final static String messages = "q_topic_messages";

	@Bean
	public Queue queueMessage() {
		return new Queue(TopicRabbitConfig.message);
	}

	@Bean
	public Queue queueMessages() {
		return new Queue(TopicRabbitConfig.messages);
	}

	/**
	 * 声明交换机
	 * @return
	 */
	@Bean
	public TopicExchange exchange() {
		return new TopicExchange("mybootexchange");
	}

	/**
	 * 绑定队列到交换机并指定routingkey
	 * @param queueMessage
	 * @param exchange
	 * @return
	 */
	@Bean
	public Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
		return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
	}

	@Bean
	public Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
		return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
	}
}
